<?php
//
class Age extends CI_Controller
{ // this controller inherits from CI_Controller

	/*
	public function __construct()
	{
		parent::__construct();
		$this->load->model('age_model');
	}
	*/
	
	// reusable view (NOT NEEDED. Using redirect instead)
	//
	function load_home_view()
	{
		$data['title'] = 'Home';
		$this->load->view('templates/header', $data);
		$this->load->view('pages/home', $data);
		$this->load->view('templates/footer', $data);
	}
	
	
	public function index()
	{
		// required
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->library('session');
		$this->load->helper('cookie');
		
		// validation
		//$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('birthday_month', 'Month', 'trim|required|xss_clean');
		$this->form_validation->set_rules('birthday_day', 'Day', 'trim|required|xss_clean');
		$this->form_validation->set_rules('birthday_year', 'Year', 'trim|required|xss_clean');
		$this->form_validation->set_rules('remember_me', 'Remember Me', 'trim'); // does not ouput error but preserves checkbox state (checked | unchecked)
		//
		// override error messages (you can also edit "language/english/form_validation_lang.php")
		//$this->form_validation->set_message('required', 'Your custom message here');
		//
		$this->form_validation->set_error_delimiters('<li>', '</li>');
		


		// Check if 'remember_me' cookie IS set 
		//
		if (get_cookie('remember_me')) 
		{
			// If Age IS validated, redirect to home
			//
			//$this->load_home_view();
			redirect('home','refresh');
		}
		
		

		// Check if age is validated first 
		//
		$age_validation = $this->session->userdata('age_validation');
		//
		if ($age_validation) 
		{
			// If Age IS validated, redirect to home
			//
			//$this->load_home_view();
			redirect('home','refresh');
		}

		
		// Show age view (form) is age has not been validated ('age_validation' session has not been set yet)
		//
		if ($this->form_validation->run() === FALSE)
		{
			// If errors, load age view 
			//
			$data['title'] = 'Age Validation';
			$this->load->view('templates/header_blank', $data);
			$this->load->view('pages/age', $data);
			$this->load->view('templates/footer_blank', $data);
		}
		else
		{
			// set cookie if checkbox IS checked
			//
			$checked = $this->input->post('remember_me');
			if((int) $checked == 1)
			{
				$cookie = array(
					'name'   => 'remember_me',
					'value'  => '1',
					'expire' => '31536000', //seconds (1 year)
					//'domain' => '.some-domain.com',
					//'path'   => '/',
					//'prefix' => 'cbb_',
					//'secure' => TRUE
				);
				$this->input->set_cookie($cookie); 
			}
			
			// set 'age_validation' session if form validates correctly
			//
			$this->session->set_userdata(array(
							'age_validation'       => 'TRUE',
							));
			
			// If no errors, redirect (Change View) to Home
			//
			//$this->load_home_view();
			redirect('home','refresh');
		}
	}
}